package com.example.springbootlearn.util;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.*;

// 基于dom4j的工具包
public class Dom4jUtil {
    /**
     * 通过文件获取domcument文档对象
     * @param path 文件路径
     * @return 文档对象
     */
    public static Document getXMlByFilePath(String path){
        if (null == path) {
            return null;
        }
        Document document = null;
        SAXReader saxReader = new SAXReader();
        try {
            document = saxReader.read(path);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return document;
    }
    /**
     * 获得文档中某元素内某属性的值和元素的文本信息
     * @param document 文档
     * @param elementName 元素名
     * @param attname 属性名
     * @return
     */
    public static Map<String,String> Elements2Map(Document document,String elementName,String attname){
        List<Element> elements = document.getRootElement().elements(elementName);
        HashMap<String, String> map = new HashMap<>();
        for (Element element: elements) {
            String key = element.attribute(attname).getValue();
            String value = element.getTextTrim();
            map.put(key,value);
        }
        return map;
    }

    /**
     * 针对mapper.xml文件 获取映射信息并存进map集合中
     * @param document 文档
     * @return
     */
    public static Map<String,String> Elements2Map(Document document){
        Element classElement = document.getRootElement().element("class");
        HashMap<String, String> map = new HashMap<>();

        Element iElement = classElement.element("id");
        String key = iElement.attribute("name").getValue();
        String value = iElement.attribute("column").getValue();
        map.put(key, value);

        List<Element> elementList = classElement.elements();
        for (Element element:elementList) {
            String key1 = element.attribute("name").getValue();
            String value1 = element.attribute("column").getValue();
            map.put(key1, value1);
        }
        return map;
    }

    /**
     * 针对mapper.xml文件，获得主键的映射信息并存到Map集合中
     *
     * @param document xml文档对象
     * @return 返回一个Map集合
     */
    public static Map<String, String> ElementsID2Map(Document document) {
        Element classElement = document.getRootElement().element("class");
        Map<String, String> mapping = new HashMap<>();

        Element idElement = classElement.element("id");
        String idKey = idElement.attribute("name").getValue();
        String idValue = idElement.attribute("column").getValue();
        mapping.put(idKey, idValue);

        return mapping;
    }
    /**
     * 获得某文档中某元素内某属性的值
     *
     * @param document    xml文档对象
     * @param elementName 元素名
     * @param attrName    属性名
     * @return 返回一个Set集合
     */
    public static Set<String> Elements2Set(Document document, String elementName, String attrName) {
        List<Element> mappingList = document.getRootElement().elements(elementName);
        Set<String> mappingSet = new HashSet<>();
        for (Element element : mappingList) {
            String value = element.attribute(attrName).getValue();
            mappingSet.add(value);
        }
        return mappingSet;
    }

    /**
     * 获取文档中某元素某属性的值
     * @param document
     * @param elementName
     * @param attrname
     * @return
     */
    public static String getPropValue(Document document,String elementName,String attrname) {
        Element element = document.getRootElement().elements(elementName).get(0);
        return element.attribute(attrname).getValue();
    }
}
